/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.rankingManager;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.sql.*;

import org.volume4.utils.ServletUtilities;

/**
 * Created by IntelliJ IDEA.
 * User: Schalk
 * Date: 2006/02/13
 * Time: 08:59:46
 * To change this template use File | Settings | File Templates.
 */

public class AddPoints extends HttpServlet {
    public String DRIVER, URL, USER, PASS;
    public String app_root, messageFailed, messageSuccess;
    public String mem_number, competition, points;

    public void init() throws ServletException {
        ServletContext context = getServletContext();
        DRIVER = context.getInitParameter("driver");
        URL = context.getInitParameter("mc_dburl");
        USER = context.getInitParameter("mc_dbuser");
        PASS = context.getInitParameter("mc_dbpass");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        Connection con = null;
        // NOTE: Find a better place to store these values.
        // Probably a Spring properties file?
        app_root = request.getContextPath();
        messageSuccess = "New competition points recorded. Awaiting activation by admin.";
        messageFailed = "Add competition points failed.";

        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL, USER, PASS);

            //Get all data from form.
            mem_number = ServletUtilities.filter(request.getParameter("mem_number"));
            competition = ServletUtilities.filter(request.getParameter("competition"));
            points = ServletUtilities.filter(request.getParameter("points"));

            String updateData = "UPDATE ab_leader_board " +
                    "SET " + competition + " = '" + points + "'" +
                    " WHERE mem_number = '" + mem_number + "'";

            PreparedStatement stmt = con.prepareStatement(updateData);

            int result = stmt.executeUpdate();
            response.sendRedirect(app_root + "/application/application_messages/process_result.jsp?message=" + messageSuccess);


        } catch (SQLException ex) {
            System.out.println("\nERROR:------ SQLException -----\n");
            while (ex != null) {
                System.out.println("Message: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("ErrorCode :" + ex.getErrorCode());
                ex = ex.getNextException();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (con != null)
                    con.close();
            } catch (SQLException ex) {
                System.out.println("\nERROR:------ SQLException -----\n");
                System.out.println("Message: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("ErrorCode :" + ex.getErrorCode());
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}